﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace Senthil.Mobile.WhereAmI
{
    [Serializable]
    public class Settings
    {
        static string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Program.ShortApplicationName);
        const string settingsFileName = "wamisettings.xml";

        private bool autoRespondToSMS;

        public bool AutoRespondToSMS
        {
            get { return autoRespondToSMS; }
            set { autoRespondToSMS = value; }
        }

        private string configuredMessageText = "wami rocks";

        public string ConfiguredMessageText
        {
            get { return configuredMessageText; }
            set { configuredMessageText = value; }
        }
        private string[] configuredUsers = new string[]{};

        public string[] ConfiguredUsers
        {
            get { return configuredUsers; }
            set { configuredUsers = value; }
        }

        bool playSoundsOnEvents;

        public bool PlaySoundsOnEvents
        {
            get { return playSoundsOnEvents; }
            set { playSoundsOnEvents = value; }
        }

        string soundPlayLocationGroup = "";
        public string SoundPlayLocationGroupName
        {
            get { return soundPlayLocationGroup; }
            set { soundPlayLocationGroup = value; }
        }

        [XmlIgnore]
        public TimeSpan TimeSpanToDestination
        {
            get { return string.IsNullOrEmpty(timeSpanToDestinationAsString) ? TimeSpan.Zero : TimeSpan.Parse(timeSpanToDestinationAsString); }
            set { TimeSpanToDestinationAsString = value.ToString(); }
        }

        string timeSpanToDestinationAsString = TimeSpan.FromMinutes(30).ToString();
        public string TimeSpanToDestinationAsString
        {
            get { return timeSpanToDestinationAsString; }
            set { timeSpanToDestinationAsString = value; }
        }

        string pathToSoundFile = @"\Windows\Alarm-WakeUp.wma";

        public string SoundFilePath
        {
            get { return pathToSoundFile; }
            set { pathToSoundFile = value; }
        }

        public Settings()
        { }

        public static Settings Load()
        {
            CheckAndCreateDir();

            string finalPath = Path.Combine(settingsPath, settingsFileName);

            if (!File.Exists(finalPath))
            {
                Settings s = new Settings();
                s.Save();
                return s;
            }
            else
            {
                using (StreamReader reader = new StreamReader(finalPath))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(Settings));
                    return (Settings) xs.Deserialize(reader);
                }
            }
        }

        private static void CheckAndCreateDir()
        {
            if (!Directory.Exists(settingsPath))
            {
                Directory.CreateDirectory(settingsPath);
            }
        }

        public void Save()
        {
            CheckAndCreateDir();
            string finalPath = Path.Combine(settingsPath, settingsFileName);

            using (StreamWriter writer = new StreamWriter(finalPath))
            {
                XmlSerializer xs = new XmlSerializer(typeof(Settings));
                xs.Serialize(writer, this);
            }
        }
    }
}
